﻿using System;
using System.Collections.Generic;
using System.Text;
using Ghasam.Business.DAO;
using Ghasam.Business.Domain.Entity;
using Spring.Transaction.Interceptor;

namespace Ghasam.Business.Services.Impl
{
    public class InvoiceService : IInvoiceService
    {
        private IInvoiceDao invoiceDao;

        public IPaymentTypeDao PaymentTypeDao { get; set; }

        public ICustomerDao CustomerDao { get; set; }

        public IPaymentDao PaymentDao { get; set; }

        public IAccountingDao AccountingDao { get; set; }

        public IInvoiceDao InvoiceDao
        {
            get { return invoiceDao; }
            set { invoiceDao = value; }
        }

        #region IInvoiceService Members

        public int CountInvoices(Invoice exampleInvoice)
        {
            return invoiceDao.CountInvoicesByExample(exampleInvoice);
        }

        public PaymentType GetPaymentType(int id) {
            return PaymentTypeDao.FindById(id);
        }

        public IList<Invoice> FindInvoices(Invoice exampleInvoice, int pageSize, int pageNumber, DateTime lastUpdateStartDate, DateTime lastUpdateEndDate)
        {
            return invoiceDao.GetInvoicesByExample(exampleInvoice, pageSize, pageNumber, lastUpdateStartDate, lastUpdateEndDate);
        }

        public Invoice FindInvoice(int id)
        {
            return invoiceDao.FindById(id);
        }

        [Transaction(ReadOnly = false)]
        public void NewInvoice(Invoice invoice, bool isCash)
        {
            long serial = invoiceDao.GetMaxSerialByFormal(invoice.IsFormal.Value) + 1;

            invoice.Revision = 1;
            invoice.Serial = serial;

            if ( isCash )
                invoice.PaymentType = PaymentTypeDao.FindById(1);
            else
                invoice.PaymentType = PaymentTypeDao.FindById(0);

            invoiceDao.SaveOrUpdate(invoice);

            if (isCash) {
                PaymentCash payment = new PaymentCash();
                payment.Amount = invoice.TotalPrice.Value;
                payment.Invoice = invoice;
                payment.LastUpdate = DateTime.Now;
                payment.OneTimeCustomer = invoice.OneTimeCustomer;
                payment.PaymentDate = DateTime.Now;
                payment.PaymentType = PaymentTypeDao.FindById(1);
                PaymentDao.SaveOrUpdate(payment);
            }
            else { 
                Accounting accounting = new Accounting();
                accounting.AccountingDate = DateTime.Now;
                accounting.CreateDate = DateTime.Now;
                accounting.Credit = 0;
                accounting.Debit = invoice.TotalPrice.Value;
                accounting.Description = "بدهی فاکتور شماره " + invoice.Serial;
                accounting.IsFormal = invoice.IsFormal.Value;
                AccountingDao.SaveOrUpdate(accounting);

                Customer customer = invoice.Customer;
                customer.Balance = customer.Balance.Value - invoice.TotalPrice.Value;
                customer.LastUpdate = DateTime.Now;
                CustomerDao.SaveOrUpdate(customer);
            }
        }

        public IList<Invoice> SortInvoice(Invoice exampleInvoice, int pageSize, int pageNumber, DateTime lastUpdateStartDate, DateTime lastUpdateEndDate, string param, bool asc)
        {
            return InvoiceDao.SortInvoice(exampleInvoice, pageSize, pageNumber, lastUpdateStartDate, lastUpdateEndDate,param, asc);
        }

        #endregion
    }
}
